﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aker.DevTeam;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraRichEdit;

namespace Aker
{
    public partial class HtmlEditor : XForm
    {
        public HtmlEditor(string Header, string SubjectLabel, string HTML, string Subject)
        {
            InitializeComponent();
            this.Text = Header;
            this.HTML = HTML;
            this.Subject = Subject;
            labelControl1.Text = SubjectLabel;
        }

        private void HtmlEditor_Load(object sender, EventArgs e)
        {
            if (!AppSession.IsOnline)
            {
                editor.Enabled = false;
                bbiSave.Enabled = false;
                txtSubject.Enabled = false;
            }

            if (AppSession.Language == "TR")
            {
                bbiSave.Caption = Language.Save;
                pasteItem1.Caption = Language.Grid.Paste;
                copyItem1.Caption = Language.Grid.Copy;
                findItem1.Caption = Language.Grid.Find;
                replaceItem1.Caption = Language.Grid.Replace;
            }
        }

        private void btnSave_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (txtSubject.Text.Trim() == "" || editor.Text.Trim() == "")
                XtraMessageBox.Show("Subject or body can not be blank !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
                this.DialogResult = DialogResult.OK;
        }

        public string HTML
        {
            get
            {
                using (MemoryStream stream = new System.IO.MemoryStream())
                {
                    editor.SaveDocument(stream, DocumentFormat.Html);
                    stream.Flush();
                    string html = Encoding.UTF8.GetString(stream.ToArray()).Replace("\r\n", "");
                    return html;
                }
            }
            set
            {
                using (Stream stream = new MemoryStream())
                {
                    TextWriter writer = new StreamWriter(stream);
                    writer.Write(value);
                    writer.Flush();
                    stream.Position = 0;
                    editor.LoadDocument(stream, DocumentFormat.Html);
                }
            }
        }

        public string Subject
        {
            get { return txtSubject.Text; }
            set { txtSubject.Text = value; }
        }
    }
}
